package com.wbao.dianniu.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.listener.IAuthCodeListener;
import com.wbao.dianniu.listener.IRegisterListener;
import com.wbao.dianniu.manager.RegisterManager;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.utils.Utils;
import com.wbao.dianniu.R;

/**
 * 注册
 * 
 * @author yejianjun
 * 
 */
public class RegisterActivity extends BaseActivity implements OnClickListener,IRegisterListener,IAuthCodeListener {

	private Button registerBtn;
	private TextView authCodeTV;
	private EditText mobileEditText;
	private EditText authCdoeEditText;
	private EditText passwordEditText;
	private EditText authPasswordEditText;
	private RegisterManager manager;
	private TextView agreementTV;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			Utils.setTranslucentStatus(this,true);
			SystemBarTintManager tintManager = new SystemBarTintManager(this);
			tintManager.setStatusBarTintEnabled(true);
			tintManager.setStatusBarTintResource(R.color.title_bg);// 通知栏所需颜色
		}
		setContentLayout(R.layout.activity_register,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
		initView();
		manager = new RegisterManager(this);
		manager.addListener(this);
		manager.addAuthListener(this);
	}

	private void initView() {
		setTitleName(R.string.register_register);
		registerBtn = (Button) findViewById(R.id.register_ok);
		authCodeTV = (TextView) findViewById(R.id.register_authCode);
		mobileEditText = (EditText) findViewById(R.id.input_Moblie);
		authCdoeEditText = (EditText) findViewById(R.id.forget_auth_code);
		passwordEditText = (EditText) findViewById(R.id.input_password);
		authPasswordEditText = (EditText) findViewById(R.id.input_auth_password);
		agreementTV = (TextView) findViewById(R.id.user_agreement_tv);
		registerBtn.setOnClickListener(this);
		authCodeTV.setOnClickListener(this);
		agreementTV.setOnClickListener(this);
	}

	CountDownTimer timer = new CountDownTimer(60000, 1000) {

	    @Override
	    public void onTick(long millisUntilFinished) {
	    	authCodeTV.setText(millisUntilFinished/1000 + "秒后重新获取");
	    }

	    @Override
	    public void onFinish() {
	    	authCodeTV.setEnabled(true);
	    	authCodeTV.setText(getResources().getString(R.string.register_get_auth_password));
	    }
	};

	protected void onDestroy() {
		super.onDestroy();
	    timer.cancel();
	};
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		String mobile = mobileEditText.getText().toString().trim(); 
		String psd = passwordEditText.getText().toString().trim(); 
		String authPsd = authPasswordEditText.getText().toString().trim();
		String authCode = authCdoeEditText.getText().toString().trim();
		switch (v.getId()) {
		case R.id.register_ok:
			if(authMobile(mobile) && authPsd(psd,authPsd,authCode)){
				manager.register(this, mobile,authCode,psd);
			}
			break;
		case R.id.register_authCode:
			//获取验证码
			if(authMobile(mobile)){
				v.setEnabled(false);
				authCodeTV.setBackgroundResource(R.drawable.get_code_unpress);
				timer.start();
				manager.getAuthCode(this, mobile);
			}
			break;
		case R.id.user_agreement_tv:
			Intent intent = new Intent();
			intent.setClass(this, UserAgreementActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}

	private boolean authPsd(String password,String authPassword,String authCode){
		boolean ret = false;
		if(TextUtils.isEmpty(password)){
			toastString(getResources().getString(R.string.login_error_3));
			return ret;
		}else if(password.length() < 6){
			toastString(getResources().getString(R.string.register_error_2));
			return ret;
		}
		
		if(TextUtils.isEmpty(authPassword)){
			toastString(getResources().getString(R.string.register_error_3));
			return ret;
		}else if (!authPassword.equals(password)) {
			toastString(getResources().getString(R.string.register_error_4));
			return ret;
		}
		if(TextUtils.isEmpty(authCode)){
			toastString(getResources().getString(R.string.register_error_5));
			return ret;
		}
		
		return true;
	}
	
	private boolean authMobile(String mobile){
		boolean ret = false;
		if(null == mobile){
			toastString(getResources().getString(R.string.register_error_1));
			return ret;
		}else {
			boolean ismobile = mobile.matches("^(13|15|18|14|17)\\d{9}$");
			if(ismobile){
				ret = true;
			}else{
				toastString("请输入正确的手机号码");
			}
			return ret;
		}
	}
	
	private void toastString(String message){
		Notification.toast(RegisterActivity.this, message);
	}

	@Override
	public void onFailure(int errorCode, String errorMsg) {
		toastString(errorMsg == null ? "":errorMsg);
	}

	@Override
	public void onSuccess() {
		toastString("注册成功");
		finish();
	}

	@Override
	public void onAuthCodeFailure(int errorCode, String errorMsg) {
		toastString(errorMsg);
	}

	@Override
	public void onAuthCodeSuccess(Object objdata) {
	}
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		}
		public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		}
	
}
